const app = getApp()
const mockData = require('../../utils/data.js')

Page({
  data: {
    order: null,
    selectedReason: ''
  },
  
  onLoad(options) {
    const orderId = parseInt(options.orderId)
    const orders = app.globalData.orders || []
    const order = orders.find(o => o.id === orderId) || {
      id: orderId,
      products: [mockData.products[0]],
      totalPrice: 1909
    }
    
    this.setData({ order })
  },
  
  selectReason(e) {
    const reason = e.currentTarget.dataset.reason
    this.setData({
      selectedReason: reason
    })
  },
  
  submit() {
    if (!this.data.selectedReason) {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none'
      })
      return
    }
    
    wx.showModal({
      title: '提示',
      content: '退款申请一旦提交,将不允许修改',
      success: (res) => {
        if (res.confirm) {
          // 更新订单状态
          const orders = app.globalData.orders || []
          const index = orders.findIndex(o => o.id === this.data.order.id)
          if (index !== -1) {
            orders[index].status = 'refunded'
            app.globalData.orders = orders
            wx.setStorageSync('orders', orders)
          }
          
          wx.showToast({
            title: '申请已提交',
            icon: 'success'
          })
          
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
        }
      }
    })
  }
})


